package com.woniuxy.yuekeuser.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.yuekeuser.controller.Form.CampsiteForm;
import com.woniuxy.yuekeuser.controller.Info.CampsiteInfo;
import com.woniuxy.yuekeuser.dao.ElasticMapper;
import com.woniuxy.yuekeuser.model.Campsite;
import com.woniuxy.yuekeuser.dao.CampsiteMapper;
import com.woniuxy.yuekeuser.model.CampsitePicture;
import com.woniuxy.yuekeuser.model.HotelCar;
import com.woniuxy.yuekeuser.service.ICampsitePictureService;
import com.woniuxy.yuekeuser.service.ICampsiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.naming.InitialContext;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@Service
public class CampsiteServiceImpl extends ServiceImpl<CampsiteMapper, Campsite> implements ICampsiteService {
    @Autowired
    private CampsiteMapper campsiteMapper;
    @Autowired
    private ICampsitePictureService iCampsitePictureService;

    @Autowired
    private ElasticMapper elasticMapper;

    public int findcampsiteIdByuserId(int id){
        System.out.println(id);
        return campsiteMapper.findcampsiteIdByuserId(id);
    }

    @Override
    public String findcampsiteNameByuserId(int id) {
        return campsiteMapper.findcampsiteNameByuserId(id);
    }

    @Override
    public IPage<CampsiteForm> findAllCampsiteList(Page<CampsiteForm> page, String cityName,String findCampsite, int heat, int rating) {
        IPage<CampsiteForm> allCampsiteList = campsiteMapper.findAllCampsiteList(page, cityName,findCampsite, heat, rating);
        List<CampsiteForm> records = allCampsiteList.getRecords();
        for (int i = 0; i < records.size(); i++) {
            CampsiteForm campsiteForm = records.get(i);
            List<CampsitePicture> campsitePictures = iCampsitePictureService.getCampsitePictureByCampsiteId(campsiteForm.getCampsiteId());
            CampsitePicture campsitePicture = campsitePictures.get(0);
            campsiteForm.setCampsitePictureAddress(campsitePicture.getCampsitePictureAddress());
        }
        return allCampsiteList;
    }

    @Override
    public Campsite findCampsiteById(int campsiteId) {
        return campsiteMapper.selectById(campsiteId);
    }

    @Override
    public void addCampsiteHeat(Campsite campsite) {
        campsiteMapper.updateById(campsite);
    }

    @Override
    public CampsiteInfo nowCampsiteById(int campsiteId) {
        CampsiteInfo campsiteInfo = campsiteMapper.nowCampsiteById(campsiteId);
        campsiteInfo.setCampsitePictureAddress(iCampsitePictureService.getOneCampsitePictureAddressById(campsiteId));
        return campsiteInfo;
    }

    @Override
    public List<Campsite> searchCampsiteByKeyWords(String keyWord) {
        List<HotelCar> hotelCars = elasticMapper.searchByKeyWords(keyWord);
        Set<Campsite> campsiteSet = new HashSet<>();

        for (HotelCar hotelCar : hotelCars) {
            campsiteSet.add(campsiteMapper.selectById(hotelCar.getCampsiteId()));
        }
        return new ArrayList<>(campsiteSet);
    }

    @Override//获取所有的营地列表通过热度
    public List<CampsiteForm> getAllCampsiteByHeat() {
        //根据热度排名营地
        List<Campsite> allCampsiteByHeat = campsiteMapper.getAllCampsiteByHeat();
        List<CampsiteForm> campsiteFormList = new ArrayList<>();
        for (int i = 0; i <allCampsiteByHeat.size() ; i++) {
            Campsite campsite = allCampsiteByHeat.get(i);
            CampsiteForm campsiteForm = new CampsiteForm();
            //根据当前营地 获取所有的图片对象
            List<CampsitePicture> campsitePictureByCampsiteId = iCampsitePictureService.getCampsitePictureByCampsiteId(campsite.getCampsiteId());
//            获取当前营地的第一张图片对象
            CampsitePicture campsitePicture = campsitePictureByCampsiteId.get(0);
            campsiteForm.setUserId(campsite.getUserId());
            campsiteForm.setCampsitePictureAddress(campsitePicture.getCampsitePictureAddress());
            campsiteForm.setCampsiteActivity(campsite.getCampsiteActivity());
            campsiteForm.setCampsiteAddress(campsite.getCampsiteAddress());
            campsiteForm.setCampsiteArea(campsite.getCampsiteArea());
            campsiteForm.setCampsiteFacilities(campsite.getCampsiteFacilities());
            campsiteForm.setCampsiteHeat(campsite.getCampsiteHeat());
            campsiteForm.setCampsiteId(campsite.getCampsiteId());
            campsiteForm.setCampsiteName(campsite.getCampsiteName());
            campsiteForm.setCampsiteRating(campsite.getCampsiteRating());
            campsiteFormList.add(campsiteForm);
        }
        return campsiteFormList;
    }
}
